//
//  NSData+DateCompare.m
//  CLExtension
//
//  Created by CharlieLiao on 2017/8/7.
//  Copyright © 2017年 Charlie. All rights reserved.
//

#import "CLDateCompare.h"
#import "NSDate+CLCalendar.h"

@implementation NSDate (CLDateCompare)

/**
 * 当前日期是否比指定的日期早
 *
 * @param date2 比较的时间
 * @return 是否早
 * @retval YES 表示早
 * @retval NO 表示相等或更新
 */
- (BOOL)isEarlier:(NSDate*)date2
{
    return ([self compare:date2] == NSOrderedAscending);
}

///
- (BOOL)isToDay
{
    NSCalendar *calendar = [NSDate cl_Calendar];
    return [calendar isDateInToday:self];
}

///
- (BOOL)isYesterday
{
    NSCalendar *calendar = [NSDate cl_Calendar];
    return [calendar isDateInYesterday:self];
}

///
- (BOOL)isTomorrow
{
    NSCalendar *calendar = [NSDate cl_Calendar];
    return [calendar isDateInTomorrow:self];
}

///// 是否为本周
//- (BOOL)isThisWeek
//{
//    NSDate *nowDate = [NSDate date];
//    NSCalendar *calendar = [NSDate cl_Calendar];
//    calendar.firstWeekday = 2;
//    
//    NSInteger nowYear = [calendar component:NSCalendarUnitYear fromDate:nowDate];
//    NSInteger year = [calendar component:NSCalendarUnitYear fromDate:self];
//    if (nowYear == year) {
//        NSInteger nowWeek = [calendar component:NSCalendarUnitWeekOfYear fromDate:nowDate];
//        NSInteger week = [calendar component:NSCalendarUnitWeekOfYear fromDate:self];
//        return nowWeek == week;
//    }
//    
//    return false;
//}

// 两个时间是否为同一天
- (BOOL)isSameDayWithDate:(NSDate*)date
{
    NSCalendar *calendar = [NSDate cl_Calendar];
    return [calendar isDate:self inSameDayAsDate:date];
}

/// 获取两个时间相差的天数
- (NSInteger)numberOfDaysDifference:(NSDate *)aDate
{
    NSCalendar *calendar = [NSDate cl_Calendar];
    
    // 先把时分秒统一为0点
    NSDate *selfDate = [calendar startOfDayForDate:self];
    NSDate *otherDate = [calendar startOfDayForDate:aDate];
    
    // 再进行比较
    unsigned int unitFlags = NSCalendarUnitDay;
    NSDateComponents *comps = [calendar components:unitFlags fromDate:selfDate toDate:otherDate options:kNilOptions];
    
    return [comps day];
}

/// 在当前日期上增加指定的月数，如果参数month为负数，则会返回减去月数后的日期
- (NSDate *)addMonth:(NSInteger)month
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMonth:month];
    NSCalendar *calender = [NSDate cl_Calendar];
    NSDate *reDate = [calender dateByAddingComponents:comps toDate:self options:kNilOptions];
    return reDate;
}

/// 在当前日期上增加指定的天数，如果参数day为负数，则会返回减去天数后的日期
- (NSDate *)addDay:(NSInteger)day
{
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:day];
    NSCalendar *calender = [NSDate cl_Calendar];
    NSDate *reDate = [calender dateByAddingComponents:comps toDate:self options:kNilOptions];
    
    return reDate;
}

@end
